package com.yj.yygh.hosp.controller;

import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yj.yygh.common.exp.YyghException;
import com.yj.yygh.common.result.R;
import com.yj.yygh.hosp.service.HospitalSetService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin    //  解决跨域问题，不能和网关一起使用
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 根据id修改医院设置状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {
        if(status != 1 && status != 0) {
            return R.error().message("状态只能是1或者0");
        }

        HospitalSet hospitalSet = hospitalSetService.getById(id);

        if(hospitalSet == null) {
            return R.error().message("要修改的医院设置不存在");
        }

        hospitalSet.setStatus(status);
        boolean b = hospitalSetService.updateById(hospitalSet);

        return b ? R.ok() : R.error();
    }

    /**
     * 根据id批量删除数据
     * @param ids
     * @return
     */
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> ids) {
        if(ids == null || ids.size() == 0) {
            return R.error().message("无实质删除数据");
        }

        boolean b = hospitalSetService.removeByIds(ids);

        return b ? R.ok() : R.error();
    }

    /**
     * 根据id修改数据
     * @param hospitalSet
     * @return
     */
    @PostMapping("updateHospSet")
    public R updateById(@RequestBody HospitalSet hospitalSet) {

        Long id = hospitalSet.getId();
        if(StringUtils.isEmpty(id)) {
            return R.error().message("id不能为空");
        }

        hospitalSet.setHoscode(null);

        boolean b = hospitalSetService.updateById(hospitalSet);

        return b ? R.ok().message("修改成功") : R.error().message("修改失败");
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("getHospSet/{id}")
    public R getById(@PathVariable("id") long id) {

        HospitalSet byId = hospitalSetService.getById(id);

        return R.ok().data("item", byId);
    }

    /**
     * 保存信息写入表
     * @param hospitalSet
     * @return
     */
    @PostMapping("saveHospSet")
    public R save(@RequestBody HospitalSet hospitalSet) {
        //  判断医院编号是否有缺失值
        if(StringUtils.isEmpty(hospitalSet.getHoscode())) {
            return R.error().message("医院编号不能为空");
        }

        //  判断医院是否已经开通设置
        QueryWrapper<HospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
        hospitalSetQueryWrapper.eq("hoscode", hospitalSet.getHoscode());
        hospitalSetQueryWrapper.last("OR ( is_deleted = 1 AND hoscode = " + hospitalSet.getHoscode() + " )");
        int count = hospitalSetService.count(hospitalSetQueryWrapper);
        if(count >= 1) {
            return R.error().message("该医院已经开通，不需要重复操作");
        }


        // 设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);

        boolean save = hospitalSetService.save(hospitalSet);

        return save ? R.ok().message("添加成功") : R.error().message("添加失败");
    }

    /**
     * 分页有条件查询
     * @param page  第几页
     * @param limit 一页中显示多少
     * @param hospitalSetQueryVo    接收前端数据
     * @return
     */
    @PostMapping("{page}/{limit}")
    public R pageQuery(@PathVariable long page
            , @PathVariable long limit
                       // 设置可以传空值，如何是空则为null
            , @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> pageparam = new Page<>(page, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        if(hospitalSetQueryVo == null) {
            hospitalSetService.page(pageparam, queryWrapper);
        } else {
            if(hospitalSetQueryVo.getHosname() != null) {
                queryWrapper.like("hosname", hospitalSetQueryVo.getHosname());
            }
            if(hospitalSetQueryVo.getHoscode() != null) {
                queryWrapper.eq("hoscode", hospitalSetQueryVo.getHoscode());
            }

            hospitalSetService.page(pageparam, queryWrapper);
        }

        List<HospitalSet> records = pageparam.getRecords();
        long total = pageparam.getTotal();

        return R.ok().data("total", total).data("records", records);
    }

    /**
     * 无条件分页查询
     * @param page
     * @param limit
     * @return
     */
    @ApiModelProperty("分页医院设置列表")
    @GetMapping("{page}/{limit}")
    public R pageList(@PathVariable long page, @PathVariable long limit) {

        Page<HospitalSet> pageparam = new Page<>(page, limit);
        hospitalSetService.page(pageparam);
        // 符合条件的总条数
        long total = pageparam.getTotal();
        // 获取到的数据
        List<HospitalSet> records = pageparam.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();

//        try {
//            int i = 1 / 0;
//        } catch (Exception e) {
//            throw new YyghException(20001, "自定义异常");
//        }



        return R.ok().data("list", list);
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public R delete(@PathVariable Long id) {
        hospitalSetService.removeById(id);
        return R.ok();
    }


}
